package com.wwp.tbshop.presenter;

import android.util.Log;

import com.wwp.tbshop.model.bean.BaseBean;
import com.wwp.tbshop.util.Constant;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by ${wenping} on 6/15/2017.
 */

public abstract class BasePresenter<T extends BaseBean> {

    protected ApiService mApiService;
    private final Retrofit mRetrofit;


    //构造方法
    public BasePresenter(){
        //创建retroift对象,builder模式;故国东来渭水流
        mRetrofit = new Retrofit
                .Builder()
                .baseUrl(Constant.HOST)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        mApiService = mRetrofit.create(ApiService.class);
    }


    protected Callback<T> mCallback = new Callback<T>() {
        @Override
        public void onResponse(Call<T> call, Response<T> response) {
             if (response.isSuccessful()){
                //返回了正确的数据
//                Log.e("response", "onSuccess");
                T responseInfo = response.body();
                onSuccess(responseInfo);

            }else {
                //服务器代码出错了
                Log.e("response", "onServerBug");
                onServerBug(response.code());
            }
        }

        @Override
        public void onFailure(Call<T> call, Throwable t) {
            //没有连上服务器
            Log.e("response", "onConnectError");
            onConnectError(t.getMessage());
        }
    };

    protected abstract void onServerBug(int code);


    protected abstract void onConnectError(String message);


    protected abstract void onSuccess(T t);

}
